<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in boxCodeList" :key="index">
					<uni-card :title="'序号:'+(index+1)" >
						<text class="uni-body">箱号：{{item}}</text><br>
					</uni-card>
				</view>				
			</scroll-view>			
		</view>
	
		<view class="footer">
			<button type="default"  @click="sumbit">确定</button>
		</view>
	
	</view>
	
	
</template>

<script>
	import Vue from 'vue'
	import { getToken } from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	const printer = uni.requireNativePlugin('LcPrinter');
	const modal = uni.requireNativePlugin('modal');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	export default {
		data() {
			return {
				baseUrl:getApp().globalData.config.baseUrl,
				userName:this.$store.state.user.name,
				orgId:getApp().globalData.config.orgId,
				boxCode:'',
				boxCodeList:[],
				detailList:[],
				dataList:[]
			}
		},
		
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = uni.getStorageSync('orgId');
			this.detailList = JSON.parse(decodeURIComponent(option.detailList));
			this.packageId = option.packageId;
			this.panelCode = option.panelCode;
		},
		
		onShow() {
			this.orgId = uni.getStorageSync('orgId');
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},
		
		onHide: function() {
			this.unregisterScan();
		},
		
		destroyed: function() {
			this.unregisterScan();
		},
		
		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
			     return false;
			}
			//that.unregisterScan();
			uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
			    delta: 1
			});
			return true;
		},
		
		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
					    let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						var result = common.scanCode(codeStr);
						if(result[0] == "C"){
							_this.boxCode = result[1];
							_this.scan(result[1]);
						}else{
							uni.showToast({
								title: '请正确扫描箱号',
								icon: 'none',
								duration: 3000
							});
							return;
						}						
					}
				});				
			},
			
			registerScan() {
				main.registerReceiver(receiver, filter);
			},
			
			unregisterScan() {
				main.unregisterReceiver(receiver);
			},
			
			scan(codeStr){
				var flag = false;
				var data = '';
				for(var i=0;i<this.detailList.length;i++){
					data = this.detailList[i];
					if(data.boxCode == codeStr && this.detailList[i].isFreeze){						
						flag = true;						
						break;
					}
				}
				if(flag){
					this.boxCodeList.push(data.boxCode);
					this.dataList.push(data);
				}else{
					uni.showToast({
						title: '此数据不存在或未添加到此托中',
						icon: 'none',
						duration: 2000
					});
					return;
				}
			},
			
			sumbit(){
				var that = this;
				//生成减码的待办任务
				uni.request({
					url:that.baseUrl+'/system/pkgline/plan',
					data:{
						boxcode : that.boxCodeList,
						productList	:that.dataList,				
						type:'reduce',
						orgId:that.orgId,
						userName:that.userName,
						panelCode:that.panelCode,
						packageId:that.packageId
					},
					method: 'POST',
					header:{
						'authorization': getToken()
					},
					dataType:'json',
					success: (res) =>{
						if(res.data.code = '200'){
							//that.unregisterScan();
							setTimeout(function(){
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							},1000);							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}																						
					}
				})
			},
		}
	}
</script>

<style lang="less">
 page {
 	width: 100%;
 	height: 100%;
 }
 
 
 .scrool-page {
 	display: flex;
 	flex-direction: column;
 	width: 100%;
 	height: 100%;
 }
 
 .header-cont {
 	width: 100%;
 	height: 110rpx;
 	line-height: 100rpx;
 	text-align: center;
 	// background: red;
 }
 
 .list-cont {
 	flex: 1;
 	position: relative;
 
 	.scrool-more {
 		position: absolute;
 		left: 0;
 		right: 0;
 		top: 0;
 		bottom: 0;
 	}
 
 	.list {
 		height: 80rpx;
 		line-height: 80rpx;
 		text-align: center;
 		background: #e0e0e0;
 
 	}
 }
 
 
 .footer {
 	color: #fff;
 	line-height: 100rpx;
 	flex: 0 0 100rpx;
 	/* 不放大不缩小固定100rpx */
 }
 
 .content{
	 font-size: 28upx;
 }
 </style>


